import {
  filterRouters,
  getFirstMenuPath,
  getMenu,
  findTreePathLists,
  getBtnListsOrPageLists,
  getPresentPageId,
  getBread,
} from "@/unit/tool";

export default {
  name: "menu",
  namespaced: true,
  state: {
    userInfo: {},
    menuLists:[], //当前app菜单
    mesMenu: [],
    moduleId: "", //当前模块id
    allAuthRoutes: [], //所有授权的路由表
    authBtnlists: [], //获取当前页面的按钮权限
    authPagelists: [], //获取当前页面的授权子页面
    firstMenuPath: "", //首菜单项路径
    activeMenuPath: "", //当前被激活的菜单
    pageId: [],
    breadList: [],
  },
  mutations: {
    // 获取当前app菜单
    saveMenuLists(state, payload) {
      let switchValue = localStorage.getItem('switchValue')
      if(switchValue && switchValue == 1) {
        let arr = getMenu(payload, "Mes后台管理");
        let pathArr = [];
        arr.forEach((item) => {
          if(item.childList) {
            item.childList.forEach((bItem) => {
              pathArr.push(bItem)
            })
          }
        });
        state.menuLists = pathArr;
      } else {
        state.menuLists = getMenu(payload, "Mes后台管理");
      }
      
      state.firstMenuPath = `${getFirstMenuPath(state.menuLists)}`;
      let AuthRoutes = filterRouters(payload, "2");
      state.allAuthRoutes = AuthRoutes;
      // this.$forceUpdate();
    },
    // 获取当前模块id
    setActiveMenuPath(state, payload) {
      let { path } = payload;
      state.activeMenuPath = path;
      state.moduleId = findTreePathLists(state.menuLists, path)[0];
      let menuArr = JSON.parse(localStorage.getItem("menuArr"))[0].childList;
      state.pageId = getPresentPageId(menuArr, state.firstMenuPath);
      state.breadList = getBread(state.menuLists, state.pageId[0]);
    },
    // 获取当前面包屑
    setPageId(state, payload) {
      let menuArr = JSON.parse(localStorage.getItem("menuArr"))[0].childList;
      state.pageId = getPresentPageId(menuArr, payload);
      state.breadList = getBread(menuArr, state.pageId[0]);
    },
    getAuthBtnlists(state, payload) {
      let { path } = payload;
      state.authBtnlists = getBtnListsOrPageLists(
        state.menuLists,
        path,
        "3"
      ).map((item) => item.path);
      state.authPagelists = getBtnListsOrPageLists(state.menuLists, path, "2");
    },
    setUserInfo(state, payload) {
      state.userInfo = payload;
    },
  },
};
